package com.yanqu.road.dao.impl.activity.platform;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.platform.UserPlatformAntiFraudData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class UserPlatformAntiFraudDataDaoImpl extends TempDataObjectDao<UserPlatformAntiFraudData> {


    public static String getUserPlatformAntiFraudDataSql = "select * from `t_u_anti_fraud_user_data` where `userId`=? and `activityId`=?";
    public UserPlatformAntiFraudData getUserPlatformAntiFraudData(long userId,int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserPlatformAntiFraudDataSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertUserPlatformAntiFraudDataSql = "insert into `t_u_anti_fraud_user_data`(userId,activityId,getReward) values (?,?,?)";

    @Override
    protected boolean addData(UserPlatformAntiFraudData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getGetReward(), ",")));
        if (getDb().executeNoneQuery(insertUserPlatformAntiFraudDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserPlatformAntiFraudDataSql = "update `t_u_anti_fraud_user_data` set getReward=? where userId=? and activityId=?";

    @Override
    protected boolean updateData(UserPlatformAntiFraudData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getGetReward(), ",")));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateUserPlatformAntiFraudDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserPlatformAntiFraudData parseData(ResultSet rs) throws SQLException {
        UserPlatformAntiFraudData data = new UserPlatformAntiFraudData();
        data.setUserId(rs.getLong("userId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setGetReward(StringUtils.stringToIntegerList(rs.getString("getReward"), ","));

        return data;
    }
}
